package com.telecom.cms.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.telecom.cms.dao.GetConnect;
import com.telecom.cms.dao.*;

import com.telecom.cms.dto.*;


public class Initmap {

	private static HashMap bookmackmap = new HashMap();

	private static HashMap groupmap = new HashMap();

	private static HashMap atommap = new HashMap();

	public static HashMap getbookmarkMap() {

		if (null == bookmackmap || bookmackmap.size() == 0) {

			Connection con = GetConnect.getConnection();

			SEG_BOOKMARK_DEFDAO dao = new SEG_BOOKMARK_DEFDAO(con);

			List list = new ArrayList();

			list = dao.findById(2);

			for (int i = 0; i < list.size(); i++) {

				SEG_BOOKMARK_DEFDTO markdto = (SEG_BOOKMARK_DEFDTO) list.get(i);

				SEG_BIZ_GROUP_DEFDAO groupdao = new SEG_BIZ_GROUP_DEFDAO(con);

				List grouplist = new ArrayList();

				grouplist = groupdao.findById(markdto.getBOOKMARK_ID());

				bookmackmap.put(markdto.getBOOKMARK_NAME(), grouplist);

			}

		}

		return bookmackmap;

	}

	public static HashMap getgroupMap() {

		if (null == groupmap || groupmap.size() == 0) {

			Connection con = GetConnect.getConnection();

			SEG_BIZ_GROUP_DEFDAO dao = new SEG_BIZ_GROUP_DEFDAO(con);

			List list = new ArrayList();

			list = dao.findAll();

			for (int i = 0; i < list.size(); i++) {

				SEG_BIZ_GROUP_DEFDTO groupdto = (SEG_BIZ_GROUP_DEFDTO) list
						.get(i);

				SEG_ATOM_GROUP_RELDAO reldao = new SEG_ATOM_GROUP_RELDAO(con);

				List rellist = new ArrayList();

				rellist = reldao.findById(groupdto.getATOM_GROUP_ID());

				List atomlist = new ArrayList();

				for (int j = 0; j < rellist.size(); j++) {

					SEG_ATOM_GROUP_RELDTO reldto = (SEG_ATOM_GROUP_RELDTO) rellist
							.get(j);

					SEG_BIZ_ATOM_DEFDAO atomdao = new SEG_BIZ_ATOM_DEFDAO(
							con);

					SEG_BIZ_ATOM_DEFDTO atomdto = atomdao.findById(reldto
							.getBIZ_ATOM_ID());

					atomlist.add(atomdto);
				}

				groupmap.put(groupdto.getATOM_GROUP_NAME(), atomlist);

			}

		}

		return groupmap;

	}

	public static HashMap getatomMap() {

		if (null == atommap || atommap.size() == 0) {

			Connection con = GetConnect.getConnection();

			PreparedStatement ps = null;
			ResultSet rs = null;

			SEG_BIZ_ATOM_DEFDAO dao = new SEG_BIZ_ATOM_DEFDAO(con);

			List list = new ArrayList();

			list = dao.findATOMAll();

			for (int i = 0; i < list.size(); i++) {

				List mea = new ArrayList();

				SEG_BIZ_ATOM_DEFDTO atomdto = (SEG_BIZ_ATOM_DEFDTO) list
						.get(i);

				String tablename = atomdto.getCDE_SRC_TABLE_NAME();

				String sql = "select * from " + tablename + " order by 1";
				try {
					ps = con.prepareStatement(sql);
					rs = ps.executeQuery();

					while (rs != null && rs.next()) {

						MEADTO meadto = new MEADTO();

						if (atomdto.getSHOW_TYPE_ID() == 2
								|| atomdto.getSHOW_TYPE_ID() == 200001) {

							meadto.setITEM_ID("");
							meadto.setSTART_VAULE(rs.getString(1));
							meadto.setEND_VALUE(rs.getString(2));
							mea.add(meadto);

						} else {
							meadto.setITEM_ID(rs.getObject(1).toString());
							meadto.setSTART_VAULE(rs.getString(2));
							meadto.setEND_VALUE(null == rs.getString(3) ? "0"
									: rs.getString(3));
							mea.add(meadto);
						}
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (rs != null) {

					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (ps != null) {
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				atommap.put(atomdto.getBIZ_ATOM_NAME(), mea);

			}

		}

		return atommap;

	}

	public static void main(String[] args) {

		Initmap.getatomMap();

		Iterator it = atommap.keySet().iterator();

		while (it.hasNext()) {

			String temp = (String) it.next();
			System.out.println(temp);
			ArrayList list = (ArrayList) atommap.get(temp);
			System.out.println(list.size());
			for (int i = 0; i < list.size(); i++) {

				MEADTO meadto = (MEADTO) list.get(i);

				System.out.println(meadto.getITEM_ID() + "          "
						+ meadto.getSTART_VAULE() + "         "
						+ meadto.getEND_VALUE());

			}

		}

	}

}
